package com.qfedu.edu.result;

import lombok.Data;

import java.util.*;

/**
 * @author xiaobobo
 * @title: R
 * @projectName cd-fy-2401-third-project-parent
 * @description: TODO
 * @date 2024/9/13  14:45
 */
@Data
public class R {

    //响应的状态码
    private Integer code;

    //错误的提示信息
    private String msg;

    //响应给客户端的数据
    private Map<String, Object> dataMap = new HashMap<>();

    //不允许别人去new这个对象
    private R() {
    }

    public static R ok() {
        R r = new R();
        r.setCode(ResponseCode.SUCCESS.getCode());
        r.setMsg(ResponseCode.SUCCESS.getMsg());
        return r;
    }

    public static R error() {
        R r = new R();
        r.setCode(ResponseCode.FAIL.getCode());
        r.setMsg(ResponseCode.FAIL.getMsg());
        return r;
    }

    public static R error(ResponseCode responseCode) {
        R r = new R();
        r.setCode(responseCode.getCode());
        r.setMsg(responseCode.getMsg());
        return r;
    }

    public static R error(Integer code, String msg) {
        R r = new R();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    /**
     * 放数据的问题
     *
     * @param key
     * @param val
     * @return
     */
    public R data(String key, Object val) {
        this.dataMap.put(key, val);
        return this;
    }

 /**
  * 放数据的问题
  * @param maps
  * @return
  */
 public R data(Map<String, Object> maps) {
        Set<Map.Entry<String, Object>> entries = maps.entrySet();
        Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> next = iterator.next();
            this.dataMap.put(next.getKey(), next.getValue());
        }
        return this;
    }

}
